package com.taokatao.cardmee.business.cardcode;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.db.model.Card;

import java.util.ArrayList;
import java.util.List;

public class CardCodePictureActivity extends BaseActivity {

    ViewPager vPager;

    private Card c;
    private String selectItem;
    private List<ImageView> imageList = new ArrayList<>();

    private View.OnClickListener onBackListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isLandscapeMode = true;
        isSupportTouchBack = false;
        super.onCreate(savedInstanceState);
        setContentView(vPager = new ViewPager(this));
        vPager.setBackgroundColor(0xFFFFFFFF);

        c = (Card) getIntent().getSerializableExtra("extra_card");
        selectItem = getIntent().getStringExtra("extra_select_item");

        int currIndex = 0;

        if (!TextUtils.isEmpty(c.getMCardNo())) {
            ImageView iBarcode = new ImageView(this);
            try {
                iBarcode.setOnClickListener(onBackListener);
                iBarcode.setImageBitmap(TextUtils.equals("2", c.getBarcodeStatus()) ?
                        Utils.createQRCode(c.getMCardNo()) : Utils.createBarcode(c.getMCardNo()));
            } catch (Exception e) {
                e.printStackTrace();
            }

            imageList.add(iBarcode);
        }

        if (!TextUtils.isEmpty(c.getPositiveIcon())) {
            ImageView iFront = new ImageView(this);
            Glide.with(this).load(c.getPositiveIcon()).centerCrop().into(iFront);
            iFront.setOnClickListener(onBackListener);
            imageList.add(iFront);
            if (TextUtils.equals(c.getPositiveIcon(), selectItem)) {
                currIndex = imageList.size() - 1;
            }
        }
        if (!TextUtils.isEmpty(c.getBackIcon())) {
            ImageView iBack = new ImageView(this);
            Glide.with(this).load(c.getBackIcon()).centerCrop().into(iBack);
            iBack.setOnClickListener(onBackListener);
            imageList.add(iBack);
            if (TextUtils.equals(c.getBackIcon(), selectItem)) {
                currIndex = imageList.size() - 1;
            }
        }

        vPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imageList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(imageList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(imageList.get(position));
                return imageList.get(position);
            }
        });

        vPager.setCurrentItem(currIndex);
    }


}
